package cn.liyicun.school.car2.controller.driver;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.liyicun.school.car2.base.BaseDriverController;
import cn.liyicun.school.car2.base.LoginDriver;
import cn.liyicun.school.car2.model.DriverOrderInfo;
import cn.liyicun.school.car2.model.DriverSign;
import cn.liyicun.school.car2.model.LineInfo;
import cn.liyicun.school.car2.model.PlaceInfo;
import cn.liyicun.school.car2.model.UserOrderInfo;
import cn.liyicun.school.car2.service.CacheModelService;

public class DriverOrderController extends BaseDriverController {

	public void userOrderList() {
		LoginDriver acc = getLoginAccount();
		System.out.println("--userOrderList--------");

		String sql = "select * from line_info where status =1";
		List<LineInfo> list = LineInfo.dao.find(sql);
		for (LineInfo lineInfo : list) {
			lineInfo.put("begin_place_data", CacheModelService.dao.placeInfo(lineInfo.getBeginPlace()));
			lineInfo.put("end_place_data", CacheModelService.dao.placeInfo(lineInfo.getEndPlace()));
		}
		setAttr("lines", list);

		if (acc.getSignPlace() > 0) {
			List<Record> signs = Db.find(
					"select d.*,s.create_time as sign_create_time,s.status as sign_status from driver_sign s join place_info d on s.place_id=d.id where s.dr_id=?  order by  s.id desc limit 10",
					acc.getId());
			setAttr("signs", signs);
		}
	}

	public void userOrderList2() {

		LoginDriver acc = getLoginAccount();
		// String sql1 = "select * from user_order_info where up_place_id=? and
		// status=1 order by id desc limit 200";
		// List<UserOrderInfo> list1 = UserOrderInfo.dao.find(sql1,
		// acc.getSignPlace());
		// for (UserOrderInfo lineInfo : list1) {
		//
		// if (lineInfo.getDrOrderId() == null || lineInfo.getDrOrderId() <= 0)
		// {
		// continue;
		// }
		// // String sql1 = "select i.* from line_place_rela r join place_info
		// // i on r.place_id=i.id where r.line_id=?";
		// // lineInfo.put("places", Db.find(sql1, lineInfo.getId()));
		// }
		// setAttr("userOrders", list1);

		if (acc.getSignPlace() <= 0) {
			redirect301("userOrderList?nosign=" + System.currentTimeMillis(), false);
			return;
		}
		List<Record> signs = Db.find(
				"select d.*,s.create_time as sign_create_time from driver_sign s join driver_info d on s.dr_id=d.id where s.place_id=? and s.status=1 order by  s.id desc limit 50",
				acc.getSignPlace());

		setAttr("signs", signs);

	}

	public void driverSign() {

		LoginDriver acc = getLoginAccount();
		Integer lineid = getParaToInt("id", 0);
		LineInfo line = LineInfo.dao.findById(lineid);
		verification(line == null, "线路不存在!");
		verification(line.getStatus()!=1, "线路状态不对!");
		int place_id=line.getBeginPlace();
		// DriverOrderInfo doi=

		Long count = Db.queryLong("select count(*) from driver_order_info where dr_id=? and status=1", acc.getId());
		verification(count > 0, "你有没有行程没有完成,不能打卡!");

		  count = Db.queryLong("select count(*) from driver_sign where dr_id=? and line_id=? and status=1", acc.getId(),lineid);
		verification(count > 0, "你已经在“"+line.getTitle()+"”签过到.");

		
		Db.update("update driver_sign set `status`=2 where dr_id=?", acc.getId());
		DriverSign sign = new DriverSign();
		sign.setDrId(acc.getId());
		sign.setCreateTime(new Date());
		sign.setPlaceId(place_id);
		sign.setLineId(lineid);
		sign.setStatus(1);
		sign.save();
		acc.setSignPlace(place_id);
		myRenderJsonSuccess("签到成功!");
	}

	public void selectUserUp() {

		LoginDriver acc = getLoginAccount();
		Long id = getParaToLong("id");

		UserOrderInfo info = new UserOrderInfo();
		info.setId(id);
		info.setStatus(2);
		info.setDrOrderId(acc.getId());
		info.update();
		redirect301("userOrderList2", false);

	}
}
